package org.alis.smallcc.alis.tools.lock.semaphore;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import lombok.extern.slf4j.Slf4j;

/**
 * 测试类
 * 
 * @author: luochuan
 * @date: 2020年10月29日 下午5:02:16
 */
@Slf4j
public class Main {
	public static void main(String[] args) throws InterruptedException {
		Executor executor = Executors.newFixedThreadPool(30);
		final CountDownLatch countDownLatch = new CountDownLatch(1);
		IntStream.range(0, 29).forEach(index -> executor.execute(() -> {
			try {
				countDownLatch.await();
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
			ResourceLock resourceLock = new ResourceLock();
			try {
				TimeUnit.SECONDS.sleep(1L);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
			log.info("用户{} 获取到资源", index);
			resourceLock.useResource(index, key -> log.info("用户{} 正在操作资源", index));
			log.info("用户{} 操作完毕", index);
		}));
		log.info("减锁操作");
		countDownLatch.countDown();
		TimeUnit.SECONDS.sleep(100L);

	}

}
